/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is Forte for Java, Community Edition. The Initial * Developer of the Original Code is Sun Microsystems, Inc. Portions * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved. */ package org.openide.nodes; import java.beans.Beans; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** Support for indexed properties. * * @see Node * * @author Jan Jancura * @version 0.14, Jan 20, 1998 */ public class IndexedPropertySupport extends Node.IndexedProperty { /** Instance of the bean. */ protected Object instance; /** setter method */ private Method setter; /** getter method */ private Method getter; /** indexed setter method */ private Method indexedSetter; /** indexed getter method */ private Method indexedGetter; /** Constructor. * @param instance the bean for which these properties exist * @param valueType type of the entire property * @param elementType type of one element of the property * @param getter get method for the entire property * @param setter set method for the entire property * @param indexedGetter get method for one element * @param indexedSetter set method for one element */ public IndexedPropertySupport ( Object instance, Class valueType, Class elementType, Method getter, Method setter, Method indexedGetter, Method indexedSetter) { super (valueType, elementType); this.instance = instance; this.setter = setter; this.getter = getter; this.indexedSetter = indexedSetter; this.indexedGetter = indexedGetter; } /* Setter for display name. * @param s the string */ public final void setDisplayName (String s) { super.setDisplayName (s); } /* Setter for name. * @param s the string */ public final void setName (String s) { super.setName (s); } /* Setter for short description. * @param s the string */ public final void setShortDescription (String s) { super.setShortDescription (s); } /* Can read the value of the property. * @return <CODE>true</CODE> if the read of the value is supported */ public boolean canRead () { return getter != null; } /* Getter for the value. * @return the value of the property * @exception IllegalAccessException cannot access the called method * @exception IllegalArgumentException wrong argument * @exception InvocationTargetException an exception during invocation */ public Object getValue () throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (!canRead ()) throw new IllegalAccessException (); Object validInstance = Beans.getInstanceOf (instance, getter.getDeclaringClass()); return getter.invoke (validInstance, new Object [0]); } /* Can write the value of the property. * @return <CODE>true</CODE> if the read of the value is supported */ public boolean canWrite () { return setter != null; } /* Setter for the value. * @param val the value of the property * @exception IllegalAccessException cannot access the called method * @exception IllegalArgumentException wrong argument * @exception InvocationTargetException an exception during invocation */ public void setValue (Object val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (!canWrite ()) throw new IllegalAccessException (); Object validInstance = Beans.getInstanceOf (instance, setter.getDeclaringClass()); setter.invoke (validInstance, new Object [] {val}); } /* Can read the indexed value of the property. * @return <CODE>true</CODE> if the read of the value is supported */ public boolean canIndexedRead () { return indexedGetter != null; } /* Getter for the indexed value. * @return the value of the property * @exception IllegalAccessException cannot access the called method * @exception IllegalArgumentException wrong argument * @exception InvocationTargetException an exception during invocation */ public Object getIndexedValue (int index) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (!canIndexedRead ()) throw new IllegalAccessException (); Object validInstance = Beans.getInstanceOf (instance, indexedGetter.getDeclaringClass()); return indexedGetter.invoke (validInstance, new Object [] {new Integer (index)}); } /* Can write the indexed value of the property. * @return <CODE>true</CODE> if the read of the value is supported */ public boolean canIndexedWrite () { return indexedSetter != null; } /* Setter for the indexed value. * @param val the value of the property * @exception IllegalAccessException cannot access the called method * @exception IllegalArgumentException wrong argument * @exception InvocationTargetException an exception during invocation */ public void setIndexedValue (int index, Object val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (!canIndexedWrite ()) throw new IllegalAccessException (); Object validInstance = Beans.getInstanceOf (instance, indexedSetter.getDeclaringClass()); indexedSetter.invoke (validInstance, new Object [] {new Integer (index), val}); } } /* * Log * 4 Gandalf 1.3 10/22/99 Ian Formanek NO SEMANTIC CHANGE - Sun * Microsystems Copyright in File Comment * 3 Gandalf 1.2 6/8/99 Ian Formanek ---- Package Change To * org.openide ---- * 2 Gandalf 1.1 3/17/99 Jesse Glick [JavaDoc] * 1 Gandalf 1.0 1/5/99 Ian Formanek * $ * Beta Change History: * 0 Tuborg 0.14 --/--/98 Jan Jancura inheritence repaired. */